//
//  GDFCitysMainListVC.m
//  GlobalDeliciousFood
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "GDFCitysMainListVC.h"
#import "GDFCitysTableViewCell.h"
#define kRow 10
#define kMarcio 20

// 双精度
typedef double NSTimeInterval;

@interface GDFCitysMainListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableDictionary *dataDic;

@end

@implementation GDFCitysMainListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
    [self requestData];
    
}
// 数据请求
- (void)requestData{
    // 先求出现在距离1970.1.1号的时间间隔秒数
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long int date = (long long int)time;
    NSString * timeStr = [NSString stringWithFormat:@"%lld",date];
    NSLog(@"%@",timeStr);
    [GDFRequestManager requestWithUrl:@"http://www.youyuwei.com/api/find?ywsdk_ver=20131018&app_code=com.youyuwei.yuwei.ios&oauth_timestamp=1452502840&oauth_version=1.0&oauth_nonce=205573a3d4f14b43855a290300d9b2fa&oauth_signature=kPnmxeEa%2FjZBBBPHi5iPfb7NXYc%3D&sys_ver=9.1&hardware_model=iPhone6%2C2&oauth_consumer_key=4&device_token=&ver=3&oauth_signature_method=HMAC-SHA1&debug=0&x_auth_mode=client_auth&app_ver=2.3&oauth_token=0_9837387abc30183c39&channel_id=App%20Store&start=0&open_udid=b7a1390870e6c128b6b6ec4a18878d5a2dcfc17e&device_type=ios&idfa=6C7104BC-CEC8-4DD0-A6AD-54FC76011DB7" parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"data"][@"list"];
        _dataDic = [NSMutableDictionary dictionary];
        for (NSDictionary *oneDic in array) {
            NSString *titleName = oneDic[@"title"];
            NSArray *contentArray = oneDic[@"content"];
            [_dataDic setObject:contentArray forKey:titleName];
        }
        [_tableView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
}
- (void)addTableView{
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[GDFCitysTableViewCell class] forCellReuseIdentifier:@"GDFCitysTableViewCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *imageName = @[@"iconfont-dayangzhou",@"iconfont-yazhou",@"iconfont-meiguo",@"iconfont-zhongguoditu",@"iconfont-feizhou",@"iconfont-ouzhou"];
    GDFCitysTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GDFCitysTableViewCell"];
    _tableView.rowHeight = cell.nameLabel.bottom + kRow;
    NSArray *keys = [_dataDic allKeys];
    NSString *key = keys[indexPath.section];
    NSArray *array = _dataDic[key];
    CGFloat width = (kScreenWidth - cell.imageV.right - 3 * kRow) / 3;
    for (int i = 0; i < 6; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.tag = (indexPath.section + 1) * 100 + i;
        if (i < 5) {
            NSDictionary *oneDic = array[i];
            button.frame = CGRectMake(cell.imageV.right + (width + kRow) * (i % 3), kRow + kMarcio + (i / 3) * 2 * kMarcio, (kScreenWidth - cell.imageV.right -kRow) / 3, kMarcio);
            [button setTitle:oneDic[@"name"] forState:(UIControlStateNormal)];
            
        } else {
            button.frame = CGRectMake(kScreenWidth - kMarcio - kRow, kRow + kMarcio + (i / 3) * 2 * kMarcio, kMarcio, kMarcio);
            [button setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouyou"] forState:(UIControlStateNormal)];
            
        }
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [cell addSubview:button];
    }

    cell.imageV.image = [UIImage imageNamed:imageName[indexPath.section]];
    cell.tag = indexPath.section + 1;
        cell.titleName = key;
    
    
    return cell;
}
- (void)buttonClick:(UIButton *)sender{
    NSLog(@"sender.tag = %ld",sender.tag);
    NSInteger section = sender.tag / 100 - 1;
    NSInteger row = sender.tag % 100;
    NSLog(@"section = %ld",section);
    NSLog(@"row = %ld",row);
    NSArray *keys = [_dataDic allKeys];
    NSString *key = keys[section];
    NSArray *array = _dataDic[key];
    NSDictionary *oneDic = array[row];
    GDFCitysDetailsViewController *detailsVC = [[GDFCitysDetailsViewController alloc] init];
    if (row < 5) {
        detailsVC.name = oneDic[@"name"];
    } else {
        
    }
    
    [self.navigationController pushViewController:detailsVC animated:YES];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
